package com.team24.hotel.controller;

import com.team24.hotel.domain.Hotel;
import com.team24.hotel.domain.Order;
import com.team24.hotel.domain.Room;
import com.team24.hotel.service.HotelService;
import com.team24.hotel.service.OrderService;
import com.team24.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private HotelService hotelService;
    @Autowired
    private RoomService roomService;

    @RequestMapping("/getAll")
    public String getAllOrder(Model model){
        List<Order> orderList=orderService.findAll();
        model.addAttribute("orderList",orderList);
        return "ordersList";
    }

    @RequestMapping("/edit")
    public String editList(String id,Model model){
        Order order=orderService.findByPrimaryKey(id);
        List<Hotel> hotelList=hotelService.findAll();
        List<Room> roomList=roomService.listRoom(order.getHotelId());
        model.addAttribute("order",order);
        model.addAttribute("hotelList",hotelList);
        model.addAttribute("roomList",roomList);
        return "orderEdit";
    }

    @RequestMapping("/save")
    public String saveList(Order order,String stringInTime){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=simpleDateFormat.parse(stringInTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setInTime(date);
        orderService.saveOrUpdate(order);
        return "redirect:/order/getAll";
    }

    @RequestMapping("/delete")
    public String deleteOrder(String  id){
        int del=orderService.deleteOrder(id);
        if (del==1)return "redirect:/order/listHistory";
        else return "404";
    }

    @RequestMapping("/listHistory")
    public String listHistory(Model model){
        List<Order> orderList=orderService.listHistoryOrder();
        model.addAttribute("orderList",orderList);
        return "orderListHistory";
    }

    @RequestMapping("/toNoPay")
    public String toNoPay(String id){
        orderService.updateOrderStatusToNoPay(id);
        return "redirect:/order/listHistory";
    }

    @RequestMapping("/toCancel")
    public String toCancel(String id) {
        orderService.updateOrderStatusToCancel(id);
        return "redirect:/order/listHistory";
    }

    @RequestMapping("/toCompleted")
    public String toCompleted(String id) {
        orderService.updateOrderStatusToCompleted(id);
        return "redirect:/order/listHistory";
    }

}
